package com.src2base64;

import org.junit.Test;

import java.io.*;
import java.util.Objects;

public class Src2Base64 {

    @Test
    public void excute() throws Exception {

        //需要处理的html的路径
        String xmlPath = "D:\\work-project\\project-2023\\zboy-utils\\src\\main\\resources\\static";
        File dirs = new File(xmlPath);

        int index = 0;
        for (String html : dirs.list()) {
            index++;

            File xml = new File(xmlPath + "\\" + html);
            if (!xml.isDirectory()) {

                if (html.contains("html")) {
                    System.out.println("html的序号index----:" + index);
                    System.out.println("html的html的名字----:" + html);
                    try {
                        toBase64(xmlPath + "\\" + html);
                    } catch (Exception e) {
                        System.out.println("报错html的名字====：" + html);
                        e.printStackTrace();
                    }
                }
            }
        }
//        toBase64("D:\\project\\project-2022\\demo\\src\\main\\resources\\static\\test.html");
    }

    public void toBase64(String path) throws Exception {
        File file = new File(path);
        //需求：读取文件中的数据，排序后再次写到本地文件
        //分析：
        //1.要把文件中的数据读取进来。
        BufferedReader br = new BufferedReader(new FileReader(path));

        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\zboy\\Desktop\\处理后的笔记\\" + file.getName()));

        String line = null;
        boolean flag = false;

        while ((line = (br.readLine())) != null) {
            if (line.contains("<div id='write'")) {
                flag = false;
                bw.write(line);
            }
            if (line.contains("<div class=\"typora-export-sidebar\">")) {
                flag = true;
                continue;
            }
            if (flag) {
                continue;
            }

            if (line.contains("script>(function () {")){
                break;
            }

            //<img src=
            if (line.contains("<img src=")) {

                String imgPath = src(line);

                File f = new File(path);
                String base64 = ImageToBase64(f.getParent() + "\\" + imgPath, path);

                String dataImg = "data:image/png;base64," + base64;

                //data:image/png;base64
                line = "<p><img src=\"" + dataImg + "\" referrerpolicy=\"no-referrer\" alt=\"img\"></p>";

                bw.write(line);
            } else {
                bw.write(line);
            }
            bw.newLine();
        }
        bw.close();
        br.close();
    }


    private String ImageToBase64(String imgPath, String path) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            System.out.println("报错html的名字====：" + path);
            e.printStackTrace();
        }
        // 对字节数组Base64编码
//        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过的字节数组字符串
//        System.out.println("本地图片转换Base64:" + encoder.encode(Objects.requireNonNull(data)));
        if (data != null) {
            return java.util.Base64.getEncoder().encodeToString(data);
        } else {
            return "";
        }
    }

    private static String src(String line) {
        String[] split = line.split(" ");

        for (String s : split) {
            if (s.contains("src=")) {
                int b = s.indexOf("\"");
                int e = s.indexOf("\"", b + 1);
                return s.substring(b + 1, e);
            }
        }
        return "";
    }

  /*  public static void main(String[] args) {
        String t = "<p><img src=\"images/mongodb124.png\" referrerpolicy=\"no-referrer\" alt=\"image-20210419165620340\"></p>";
        String src = src(t);
        System.out.println(src);
    }*/


}
